package com.mjfeed.adapter;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.mjfeed.activity.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

/**
 * 轮播图
 * @author zhaominglei
 * @date 2014-12-16
 * 
 */
public class BannerAdapter extends PagerAdapter {
	private View rowView;
	private Context context;
	@SuppressLint("UseSparseArrays")
	private Map<Integer, View> rowViews = new HashMap<Integer, View>();

	private int[] images = new int[]{ R.drawable.fragment_home_lunbo1, R.drawable.fragment_home_lunbo2, R.drawable.fragment_home_lunbo3 };
//	private String[] images = new String[]{ "http://meiaochina.cn:80/download.shtml?filepath=/usr/local/upload/image/indexImage/20150929/473f4a87-ab99-4c93-8f31-2269af72bdb0.png", "http://meiaochina.cn:80/download.shtml?filepath=/usr/local/upload/image/indexImage/20150903/b3ee57ae-e8b4-4c85-b446-b89b6c24ef48.jpg", "http://meiaochina.cn:80/download.shtml?filepath=/usr/local/upload/image/indexImage/20150723/9c4d66f4-7127-44f1-a1cd-8d93097ec971.png" };
	
	public BannerAdapter(Context context) {
		this.context = context;
	}

	@Override
	public void destroyItem(View view, int arg1, Object arg2) {
		((ViewPager) view).removeView((View) arg2);
	}

	@Override
	public int getCount() {
		return images.length;
	}

	@SuppressLint("InflateParams")
	@Override
	public Object instantiateItem(View view, int arg1) {
		BannerHolder holder = null;
		rowView = rowViews.get(view);
		if (rowView == null) {
			rowView = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
			holder = new BannerHolder(rowView);
			rowView.setTag(holder);
		} else {
			holder = (BannerHolder) rowView.getTag();
		}
		holder.getImageView().setBackgroundResource(images[arg1]);
//		holder.getImageView().setImageBitmap(returnBitMap(images[arg1]));
		rowViews.put(arg1, rowView);
		((ViewPager) view).addView(rowView, 0);
		return rowView;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	private class BannerHolder {
		private View view;
		private ImageView imageview;

		private BannerHolder(View v) {
			view = v;
		}

		ImageView getImageView() {
			if (imageview == null) {
				imageview = (ImageView) view.findViewById(R.id.banner);
			}
			return imageview;
		}
	}
	
    public Bitmap returnBitMap(String url){
    	URL myFileUrl = null;  
    	Bitmap bitmap = null; 
    	try {  
    		myFileUrl = new URL(url);  
    	} catch (MalformedURLException e) {  
    		e.printStackTrace();  
    	}  
    	try {  
    		HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();  
    		conn.setDoInput(true);  
    		conn.connect();  
    		InputStream is = conn.getInputStream();  
    		bitmap = BitmapFactory.decodeStream(is);  
    		is.close();  
    	} catch (IOException e) {  
    		  e.printStackTrace();  
    	}  
    	return bitmap;  
    }  	

}
